import nProgress from 'nprogress'
import router from './router'
import store from './store'
import 'nprogress/nprogress.css'
import getPageTitle from './utils/get-page-title'

const whiteList = ['/login', '/404']
router.beforeEach((to, form, next) => {
  nProgress.start()
  if (whiteList.includes(to.path)) return next()
  if (!store.state.user.token) return next('/login')
  store.dispatch('user/getUserInfo').then(() => {
    next()
  })
})
router.beforeEach((to, from, next) => {
  document.title = getPageTitle(to.meta.title)
  next()
})
router.afterEach(() => {
  nProgress.done()
})
